//
//  RCDTabBarViewController.m
//  BaseProject
//
//  Created by 任辰东 on 2018/7/12.
//  Copyright © 2018年 任辰东. All rights reserved.
//

#import "RCDTabBarViewController.h"
#import "RCDBaseNavigationController.h"
#import "UIImage+Color.h"
#import "RCDBaseViewController.h"

////首页
#import "HomePageVC.h"
////我的
#import "MineVC.h"
////通知
#import "NotificationListVC.h"

@interface RCDTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation RCDTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    if (@available(iOS 13.0, *)) {
//        UITabBarAppearance *tabbarApperrance = [[UITabBarAppearance alloc]init];
//        tabbarApperrance.backgroundColor = UIColorFromHex(kColorWhite);
//        tabbarApperrance.backgroundImage = [UIImage imageWithColor:UIColorFromHex(kColorWhite)];
//        tabbarApperrance.shadowImage = [UIImage imageWithColor:UIColorFromHex(kColorWhite)];
//        self.tabBar.standardAppearance = tabbarApperrance;
//
//    } else {
//        // Fallback on earlier versions
//    }
    [self setUpControllers];
}

- (void)setUpControllers
{
    
    HomePageVC *homeVC = [[HomePageVC alloc] init];
    RCDBaseNavigationController *nav1 = [[RCDBaseNavigationController alloc] initWithRootViewController:homeVC];

    NotificationListVC *notificationVC = [[NotificationListVC alloc] init];
    RCDBaseNavigationController *nav2 = [[RCDBaseNavigationController alloc] initWithRootViewController:notificationVC];

    MineVC *mineVC = [[MineVC alloc] init];
    RCDBaseNavigationController *nav3 = [[RCDBaseNavigationController alloc] initWithRootViewController:mineVC];

    self.viewControllers = @[nav1, nav2, nav3];
    
    [self customizeTabBar];
}

- (void)customizeTabBar
{
    self.delegate = self;
    self.tabBar.translucent = NO;
    [UITabBar appearance].translucent = NO; //这句表示取消tabBar的透明效果。
    self.tabBar.barTintColor = [UIColor whiteColor];
    self.tabBar.shadowImage = [UIImage imageWithColor:UIColorFromHex(kColorLine) size:CGSizeMake(kScreenWidth, kOnePixel)];
    self.tabBar.backgroundImage = [UIImage imageWithColor:UIColorFromHex(kColorWhite) size:CGSizeMake(kScreenWidth, kTabarHeight)];
    
    NSArray *titleArray = @[@"首页",@"通知", @"我的"];
    NSArray *normalImageNameArray = @[@"tabbar_home_n",@"tabbar_notification_n", @"tabbar_mine_n"];
    NSArray *selectedImageNameArray = @[@"tabbar_home_h",@"tabbar_notification_h", @"tabbar_mine_h"];
    
    NSUInteger index = 0;
    for (RCDBaseNavigationController *navigationController in self.viewControllers) {
        RCDBaseViewController *viewController = navigationController.viewControllers.firstObject;

        NSString *normalImageName = normalImageNameArray[index];
        NSString *selectedImageName = selectedImageNameArray[index];
        
        UIImage *normalImage = UIImageWithName(normalImageName);
        UIImage *selectedImage = UIImageWithName(selectedImageName);
        
        UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[index] image:normalImage tag:index];
        [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorFromHex(0x8e8e8e)} forState:UIControlStateNormal];
        [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: UIColorFromHex(kColorMain)} forState:UIControlStateSelected];
        
        if ([normalImage respondsToSelector:@selector(imageWithRenderingMode:)]) {
            normalImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        }
//        [tabBarItem setImageInsets:UIEdgeInsetsMake(5, 0, -5, 0)];
        tabBarItem.image = normalImage;
        tabBarItem.selectedImage = selectedImage;
        
        viewController.tabBarItem = tabBarItem;
        index++;
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    [UIView setAnimationsEnabled:false];
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [UIView setAnimationsEnabled:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
